jQuery(function($){
	

	searchRule();
	
});

function searchRule(){
	var severeStyle;
	var severity;
	var imgSrc;
	var imgTitle;
	var rule_row = '';
	var expression_row = '';
	$.ajax({
		type: "get",
		url: "/detection/rule/show",
		cache:false,
		success: function(data){
	       for(var i = 0; i < data.length; i++){    //获取后台传过来的url中的数据
	    	    var id=data[i].id;
	    	    var ruleName = data[i].ruleName;
	            var hostName = data[i].host.hostName; //拼接规则表达式用
	            var metricName = data[i].metric.metricName;
	            var funcName = data[i].checkFunc.funcName;
	            var funcParameter = data[i].funcParameter;
	            var paraUnit = data[i].paraUnit;
	            var operator = data[i].operator;
	            var triggerThreshold = data[i].triggerThreshold;
	            var expression ='{'+hostName+':'+metricName+' '+funcName+'('+funcParameter+' '+paraUnit+')}'+operator+' '+triggerThreshold;
	            var description = data[i].description;
	            var severe = data[i].severe;
	            var isEnabled = data[i].isEnabled;
	            
	            
	            switch(severe){         //对违反规则的严重程度进行判断,并进行显式转换
		    		case 1:
		    			severeStyle = 'status-metro status-low';
		    			severity = 'low';
		    			break;
		    		case 2:
		    			severeStyle = 'status-metro status-warning';
		    			severity = 'warning';
		    		 	break;
		    		case 3:
		    			severeStyle = 'status-metro status-high';
		    			severity = 'high';
		    			break;
		    		case 4:
		    			severeStyle = 'status-metro status-higher';
		    			severity = 'higher';
		    			break;
		    		case 5:
		    			severeStyle = 'status-metro status-serious';
		    			severity = 'serious';
		    			break;
		    		case 6:
		    			severeStyle = 'status-metro status-disaster';
		    			severity = 'disaster';
		    			break;
		    		default:
		    			severeStyle = 'status-metro status-def';
		    		    severity = 'normal';
	    		};
	    		switch(isEnabled){
		    		case 1:
		    			imgSrc = 'documents/images/enable.jpg';
		    			imgTitle = '启用状态，点击关闭';
		    			break;
		    		case 2:
		    			imgSrc = 'documents/images/enable.jpg';
		    			imgTitle = '关闭状态，点击启用';
		    		 	break;
		    		default://默认启用状态
		    			imgSrc = 'documents/images/enable.jpg';
	    				imgTitle = '启用状态，点击关闭';
	    		
	    		};
	    		
	    		
	            //
	            rule_row += '<tr><td style="visibility:hidden">' + id + '</td><td>' 
	            					   + ruleName + '</td><td>' 
	            					   + expression + '</td><td>' 
	            					   + description + '</td><td><span class="'+severeStyle +'">'
	            					   + severity +'</span></td><td>'
	            					   // +'<input onclick="imgBtn(this)" width="20" height="20" type="image" src="'+imgSrc+'"title="'+imgTitle+'">'
	            					   +'<button id="delBtn" onclick="deleteRow(this)" type="button" class="btn btn-default">删除</button></td</tr>';
	            
	            expression_row +='<tr><td>' +  expression + '</td></tr>';
	       };
	       $('#rule_body').html(rule_row);//将每一行的数据加载到html中
	       $('#rule_table').footable();//对footable进行初始化
	       $('#expression_body').html(expression_row);//将每一行的数据加载到html中
	       $('#expression_table').footable();//对footable进行初始化
	       
	       
		}
	});	
}

//获取当前行规则的id 删除  并且重新加载表格
function deleteRow(obj){
	var x = $(obj).parent().parent().find("td");
    var clickId =  x.eq(0).text();  //得到点击行的id值
    //$('#rule_body').removeChild(this.parentNode.parentNode);
    //alert(clickId);
    $.ajax({
		type:'post',
		url: '/detection/rule/delete',
		cache:false,
		dataType: "text",
		data : {
			'id':clickId
		},
		success : function(data) {
            alert("删除成功");
            searchRule();
        },
        error : function(jqXHR, textStatus, errorThrown){
        	/*alert(jqXHR.responseText);
            alert(jqXHR.status);
            alert(jqXHR.readyState);
            alert(jqXHR.statusText);*/
        	alert("请求失败");
        	
        }
	});
}


//启用 关闭规则的红绿按钮
function imgBtn(obj){
	var x = $(obj).parent().parent().find("td");
    var clickId =  x.eq(0).text();  //得到点击行的id值
    alert(clickId);
    $.ajax({
		type:'post',
		url: '',
		cache:false,
		data : {
			'id':clickId
		},
		success : function(data) {
            alert("规则启用成功"); 
            searchRule();//重新请求
        },
        error : function(jqXHR, textStatus, errorThrown){
        	/*alert(jqXHR.responseText);
            alert(jqXHR.status);
            alert(jqXHR.readyState);
            alert(jqXHR.statusText);*/
        	alert("规则启用失败");
        	
        }
	});
}

